package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * mgr Controller的方法
 */
@Controller
public class LoginController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @RequestMapping("/mgrLogin")
    @ResponseBody
    public AjaxResult loginPage(String username, String password) {
        AjaxResult result = null;
        try {
            loginInfoService.login(username, MD5.encoder(password),LoginInfo.USERTYPE_ADMIN);
            result = new AjaxResult("登陆成功");
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }

    @RequestMapping("/main")
    public String mainPage() {
        return "main";
    }
}
